﻿using System;
using System.Globalization;

namespace DevTeamIT
{

    public class FormatYuzde : IFormatProvider, ICustomFormatter
    {
        public FormatYuzde()
        {
        }

        public object GetFormat(Type formatType)
        {
            return (formatType == typeof(ICustomFormatter)) ? this : null;
        }

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            try
            {
                double value = double.Parse(arg.ToString());
                return value == 0.0 ? "" : value.ToString("'%' #,##0.##");

            }
            catch (Exception)
            {
                return arg.ToString();
            }
        }
    }


    public class FormatMetre : IFormatProvider, ICustomFormatter
    {
        public FormatMetre()
        {
        }

        public object GetFormat(Type formatType)
        {
            return (formatType == typeof(ICustomFormatter)) ? this : null;
        }

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            try
            {
                double value = double.Parse(arg.ToString());
                return value == 0.0 ? "" : value.ToString("#,##0.## mt'.'");

            }
            catch (Exception)
            {
                return arg.ToString();
            }
        }
    }

    public class FormatHafta : IFormatProvider, ICustomFormatter
    {
        public FormatHafta()
        {
        }

        public object GetFormat(Type formatType)
        {
            return (formatType == typeof(ICustomFormatter)) ? this : null;
        }

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            try
            {
                DateTime value = DateTime.Parse(arg.ToString());

                // Gets the Calendar instance associated with a CultureInfo.
                CultureInfo myCI = new CultureInfo("tr-TR");
                Calendar myCal = myCI.Calendar;

                // Gets the DTFI properties required by GetWeekOfYear.
                CalendarWeekRule myCWR = myCI.DateTimeFormat.CalendarWeekRule;
                DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;

                return value == null ? "" : myCal.GetWeekOfYear(value, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
                //return value == null ? "" : myCal.GetWeekOfYear(value, myCWR, myFirstDOW).ToString();
            }
            catch (Exception)
            {
                return arg.ToString();
            }
        }
    }


}